/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.comut.crud.ejb;

import com.comut.crud.bo.DriverHandler;
import com.comut.crud.bo.NotificationHandler;
import com.comut.crud.connection.MooveFleetConnectionManager;
import com.comut.crud.pojo.driver.Driver;
import com.comut.crud.ejb.interfaces.local.IDriverActionLocal;
import com.comut.crud.ejb.interfaces.remote.IDriverActionRemote;
import com.comut.crud.pojo.mobile.DriverDetail;
import com.comut.crud.pojo.mobile.DriverInfo;
import com.comut.lib.connection.MongoDBConnectionManager;
import com.comut.lib.error.ErrorCodes;
import com.comut.lib.exception.BaseException;
import com.comut.lib.exception.ObjectNotFoundException;
import java.sql.Connection;
import java.util.Date;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Stateless;
import org.apache.log4j.Logger;

/**
 *
 * @author asantos
 */
@Stateless
public class DriverBean implements IDriverActionLocal, IDriverActionRemote {

    private static Logger _logger = Logger.getLogger(DriverBean.class.getName());

    @PostConstruct
    private void driverPostConstruct() {
        _logger.info("PostConstruct");
    }

    @PreDestroy
    private void driverPreDestroy() {
        _logger.info("PreDestroy");
    }

    public DriverBean() {
    }

    public Driver insertDriver(Driver driver) {
        Connection connection = null;
        Driver toRet;
        DriverHandler th;
        try {
            connection = MooveFleetConnectionManager.instance().getConnection();
            th = new DriverHandler(connection);
            toRet = th.insertDriver(driver);
        } finally {
            MooveFleetConnectionManager.instance().freeConnection(connection);
            th = null;
        }
        NotificationHandler.notifyAboutDrivers();
        return toRet;
    }

    public void deleteDriver(Driver driver) {
        if(driver.getId()<1)
            throw new BaseException("The id must be a int bigger than 0", ErrorCodes.INVALID_ID);
        Connection connection = null;
        DriverHandler th;
        try {
            connection = MooveFleetConnectionManager.instance().getConnection();
            th = new DriverHandler(connection);
            th.deleteDriver(driver);
        } finally {
            MooveFleetConnectionManager.instance().freeConnection(connection);
            th = null;
        }
        NotificationHandler.notifyAboutDrivers();
    }

    public void updateDriver(String driver) {
        Connection connection = null;
        DriverHandler th;
        try {
            connection = MooveFleetConnectionManager.instance().getConnection();
            th = new DriverHandler(connection);
            th.updateDriver(driver);
        } finally {
            MooveFleetConnectionManager.instance().freeConnection(connection);
            th = null;
        }
        NotificationHandler.notifyAboutDrivers();
    }

    public Driver getDriver(Driver driver) {
        Connection connection = null;
        Driver toRet = null;
        DriverHandler th;
        try {
            connection = MooveFleetConnectionManager.instance().getConnection();
            th = new DriverHandler(connection);
            toRet = th.getDriver(driver);
        } finally {
            MooveFleetConnectionManager.instance().freeConnection(connection);
            th = null;
        }
        return toRet;
    }

    /**
     * @author Paulo Fournier
	 * Issue: IFL-1569
     * Gets list of drivers for user id.
     * @param userId The user's id
     * @return DriverInfo[] The list of drivers and their information
     * @throws ObjectNotFoundException
     */
    public DriverInfo[] getListDrivers(int userId) throws ObjectNotFoundException {
        DriverInfo[] ret = null;
        Connection connection = null;
        DriverHandler driverHandler;

        try {
            connection = MooveFleetConnectionManager.instance().getConnection();
            MongoDBConnectionManager mongodb = MongoDBConnectionManager.instanceFromSystemProperties();
            driverHandler = new DriverHandler(connection, mongodb);
            ret = driverHandler.getListDrivers(userId);
        } finally {
            MooveFleetConnectionManager.instance().freeConnection(connection);
            driverHandler = null;
        }

        return ret;
    }

    /**
     * @author Paulo Fournier
	 * Issue: IFL-1570
	 * Get driver's detail
     * @param userId The user id
     * @param driverId The driver id
     * @return DriverDetail The driver detail object
     * @throws ObjectNotFoundException
     */
    public DriverDetail getDriverDetail(int userId, int driverId) throws ObjectNotFoundException {
        Connection connection = null;
        DriverHandler driverHandler;
        DriverDetail ret = null;
        
        try {
            connection = MooveFleetConnectionManager.instance().getConnection();
            MongoDBConnectionManager mongodb = MongoDBConnectionManager.instanceFromSystemProperties();
            driverHandler = new DriverHandler(connection, mongodb);
            ret = driverHandler.getDriverDetail(userId, driverId);
        } finally {
            MooveFleetConnectionManager.instance().freeConnection(connection);
            driverHandler = null;
        }

        return ret;
    }
}
